package com.lizard.schedule.widget.socialshare;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.lizard.schedule.R;
import com.lizard.schedule.manager.umeng.UmengShareManager;
import com.lizard.schedule.utils.DeviceUtil;
import com.lizard.schedule.utils.ScheduleUtil;
import com.lizard.schedule.widget.ScheduleToast;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import java.util.ArrayList;
import java.util.List;

/**
 * 社会化分享PopupWindow组件
 */
public class SocialSharePopupWindow extends PopupWindow {

    private final int ANIM_TIME = 200;

    private Activity mActivity;

    private View mParent;

    private View mBottom;

    private GridView mShareGrid;

    /**
     * 状态栏
     */
    private ViewGroup mStatusBar;

    /**
     * 临时加入状态栏的view
     */
    private View mRenderView;

    /**
     * 用于防止重复收到bottom的GlobalLayout监听
     */
    private boolean mBottomGlobalLayout = false;

    /**
     * 用于防止重复执行关闭动画
     */
    private boolean mDismissed = false;

    public SocialSharePopupWindow(Activity activity, View parent){
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mActivity = activity;
        mParent = parent;
        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        View contentView = layoutInflater.inflate(R.layout.popup_social_share, null);
        setContentView(contentView);

        initView(contentView);

        //加入以下两行代码以后才能点击返回键关闭界面
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    }

    private void initView(View contentView){
        mBottom = contentView.findViewById(R.id.bottom);
        initOutView(contentView);
        setBottomOnGlobalLayoutLis();
        initShareGrid(contentView);
    }

    private void initOutView(View contentView){
        contentView.findViewById(R.id.out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    private void setBottomOnGlobalLayoutLis(){
        mBottom.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (!mBottomGlobalLayout) {
                    mBottomGlobalLayout = true;

                    // 能获取到bottom的高度后再取开启动画
                    showStartAnim();
                }
                mBottom.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });
    }

    /**
     * 初始化分享选项
     *
     * @param contentView
     */
    private void initShareGrid(View contentView){
        mShareGrid = (GridView) contentView.findViewById(R.id.share_grid);
        List<SocialBean> socials = new ArrayList<SocialBean>();
        socials.add(getWechatSocial());
        socials.add(getWxCircleSocial());
        socials.add(getQQSocial());
        socials.add(getQZoneSocial());
        socials.add(getSinaSocial());
        socials.add(getSmsSocial());
        socials.add(getCopyLinkSocial());
        socials.add(new SocialBean());
        ShareGridAdapter adapter = new ShareGridAdapter(mActivity, socials);
        mShareGrid.setAdapter(adapter);

    }

    /**
     * SocialBean:分享到微信好友
     *
     * @return
     */
    private SocialBean getWechatSocial(){
        SocialBean social = new SocialBean();
        social.setIconResId(R.mipmap.share_wechat);
        social.setTitle(mActivity.getString(R.string.share_wechat));
        social.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UmengShareManager.getInstance().shareWXFriend(mActivity);
                dismiss();
            }
        });
        return social;
    }

    /**
     * SocialBean:分享到朋友圈
     *
     * @return
     */
    private SocialBean getWxCircleSocial(){
        SocialBean social = new SocialBean();
        social.setIconResId(R.mipmap.share_wxcircle);
        social.setTitle(mActivity.getString(R.string.share_wxcircle));
        social.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UmengShareManager.getInstance().shareWXCircle(mActivity);
                dismiss();
            }
        });
        return social;
    }

    /**
     * SocialBean:分享到QQ
     *
     * @return
     */
    private SocialBean getQQSocial(){
        SocialBean social = new SocialBean();
        social.setIconResId(R.mipmap.share_qq);
        social.setTitle(mActivity.getString(R.string.share_qq));
        social.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UmengShareManager.getInstance().shareQQFriend(mActivity);
                dismiss();
            }
        });
        return social;
    }

    /**
     * SocialBean:分享到QQ空间
     *
     * @return
     */
    private SocialBean getQZoneSocial(){
        SocialBean social = new SocialBean();
        social.setIconResId(R.mipmap.share_qzone);
        social.setTitle(mActivity.getString(R.string.share_qzone));
        social.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UmengShareManager.getInstance().shareQZone(mActivity);
                dismiss();
            }
        });
        return social;
    }

    /**
     * SocialBean:分享到新浪微博
     *
     * @return
     */
    private SocialBean getSinaSocial(){
        SocialBean social = new SocialBean();
        social.setIconResId(R.mipmap.share_sina);
        social.setTitle(mActivity.getString(R.string.share_sina));
        social.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UmengShareManager.getInstance().shareSina(mActivity);
                dismiss();
            }
        });
        return social;
    }

    /**
     * SocialBean:分享到短信
     *
     * @return
     */
    private SocialBean getSmsSocial(){
        SocialBean social = new SocialBean();
        social.setIconResId(R.mipmap.share_sms);
        social.setTitle(mActivity.getString(R.string.share_sms));
        social.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(DeviceUtil.canUseSim()){
                    UmengShareManager.getInstance().shareSms(mActivity);
                } else{
                    ScheduleToast.makeScheduleToastS(R.string.no_sim).show();
                }
                dismiss();
            }
        });
        return social;
    }

    /**
     * SocialBean:复制链接
     *
     * @return
     */
    private SocialBean getCopyLinkSocial(){
        SocialBean social = new SocialBean();
        social.setIconResId(R.mipmap.share_link);
        social.setTitle(mActivity.getString(R.string.share_copy_link));
        social.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ScheduleUtil.copy(UmengShareManager.getInstance().getShareLink());
                ScheduleToast.makeScheduleToastS(R.string.share_link_copied).show();
                dismiss();
            }
        });
        return social;
    }

    @Override
    public void dismiss() {
        if(!mDismissed){
            mDismissed = true;
            showHideAnim();
        }
    }

    /**
     * 显示界面
     */
    public void show(){
        showAtLocation(mParent, Gravity.NO_GRAVITY, 0, 0);

        renderStatusBar();
    }

    /**
     * 解决4.4界面覆盖状态栏时 popupWindow没有覆盖到状态栏的问题 往状态栏里面填充进一层view
     */
    private void renderStatusBar(){
        mStatusBar = (ViewGroup)mParent.findViewById(R.id.status_bar);
        if(mStatusBar != null){
            mRenderView = new View(mActivity);
            mRenderView.setId(R.id.status_bar);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DeviceUtil.getStatusHeight(mActivity));
            mRenderView.setLayoutParams(params);
            mRenderView.setBackgroundColor(mRenderView.getResources().getColor(R.color.popup_window_bg));

            mStatusBar.addView(mRenderView);
        }
    }

    /**
     * 将填到状态栏的view移除
     */
    private void unRenderStatusBar(){
        if(mStatusBar != null){
            mStatusBar.removeView(mRenderView);
        }
    }

    private void showStartAnim(){
        Animator anim = ObjectAnimator
                .ofFloat(mBottom, "translationY", mBottom.getHeight(), 0)
                .setDuration(ANIM_TIME);
        anim.start();
    }

    private void showHideAnim(){
        Animator anim = ObjectAnimator
                .ofFloat(mBottom, "translationY", 0, mBottom.getHeight())
                .setDuration(ANIM_TIME);
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                SocialSharePopupWindow.super.dismiss();
                unRenderStatusBar();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        anim.start();
    }


}
